package com.itheima.controller;

import com.itheima.client.shoppingcartpojo.dto.ShoppingCartAddDto;
import com.itheima.client.shoppingcartpojo.dto.ShoppingCartdeleteDto;
import com.itheima.client.shoppingcartpojo.vo.ShoppingCartAddVo;
import com.itheima.client.shoppingcartpojo.vo.ShoppingCartListVo;
import com.itheima.common.JsonResult;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wuzhiyong
 */
@RestController
@RequestMapping(value = "/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCartAddDto shoppingCartAddDto
     * @return JsonResult<ShoppingCartAddVo>
     */
    @PostMapping(value = "/add")
    public JsonResult<ShoppingCartAddVo> shoppingCartAdd(@RequestBody ShoppingCartAddDto shoppingCartAddDto) {
        ShoppingCartAddVo shoppingCartAddVo = shoppingCartService.addShoppingCart(shoppingCartAddDto);
        if (shoppingCartAddVo!=null) {
            return JsonResult.success(shoppingCartAddVo, null);
        }
        return JsonResult.error("添加失败");
    }

    /**
     * 购物车列表
     *
     * @return JsonResult<List < ShoppingCartListVo>>
     */
    @GetMapping(value = "/list")
    public JsonResult<List<ShoppingCartListVo>> shoppingCartList() {
        List<ShoppingCartListVo> shoppingCartListVos = shoppingCartService.getShoppingCart();
        if (shoppingCartListVos != null) {
            return JsonResult.success(shoppingCartListVos, null);
        }
        return JsonResult.error(null);
    }

    /**
     * 清空购物车
     *
     * @return JsonResult<String>
     */
    @DeleteMapping(value = "/clean")
    public JsonResult<String> shoppingCartClean() {
        if (shoppingCartService.removeShoppingCart()) {
            return JsonResult.success("删除成功", null);
        }
        return JsonResult.error("删除失败");
    }

    /**
     * 删除购物车商品
     *

     * @param shoppingCartdeleteDto shoppingCartdeleteDto
     * @return JsonResult<String>
     */
    @PostMapping(value = "/sub")
    public JsonResult<String> shoppingCartSub( @RequestBody ShoppingCartdeleteDto shoppingCartdeleteDto) {
        if (shoppingCartService.removeShoppingCartByDishIdOrSetmenalId(shoppingCartdeleteDto)) {
            return JsonResult.success("删除成功", null);
        }
        return JsonResult.error("购物车中暂无商品");
    }
}
